package morning.cat;

import morning.cat.service.MyService;
import morning.cat.service.UserService;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2020/1/12 3:20 PM
 */
public class Starter4AsXml {

	public static void main(String[] args) {
		test3();
	}

	private static void test1() {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/bean.xml");

		MyService myService = applicationContext.getBean(MyService.class);
		System.out.println(myService);
		myService.getUserService().sayHello();


	}


	private static void test2() {
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext("META-INF/bean.xml");
		// IOException parsing XML document from file [/Users/kevin/Documents/gitee/spring-framework/META-INF/bean.xml];
	}

	private static void test3() {
		GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext("META-INF/bean.xml");
		MyService myService = applicationContext.getBean(MyService.class);
		myService.getUserService().sayHello();

		String[] beanDefinitions = applicationContext.getBeanDefinitionNames();
		for (String beanDefinitionName : beanDefinitions) {
			if (!beanDefinitionName.contains("MyService") && !beanDefinitionName.equalsIgnoreCase(UserService.class.getSimpleName())) {
				continue;
			}
			BeanDefinition beanDefinition = applicationContext.getBeanDefinition(beanDefinitionName);
			System.out.println(beanDefinition);
		}
	}

	private static void test4() {
		GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext("META-INF/bean.xml");
		MyService myService = applicationContext.getBean(MyService.class);

		MyService myService2 = applicationContext.getBean(MyService.class);

		System.out.println(myService == myService2);
		System.out.println(myService.getUserService() == myService2.getUserService());

		UserService userService = applicationContext.getBean(UserService.class);
		UserService userService2 = applicationContext.getBean(UserService.class);
		System.out.println(userService == userService2);
	}
}
